package com.hnucm.c201901020121.Utils.Adapter;


import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.hnucm.c201901020121.Fragment.Lobby.BringFoodFragment;
import com.hnucm.c201901020121.Fragment.Lobby.BringFruitFragment;
import com.hnucm.c201901020121.Fragment.Lobby.PickUpExpressFragment;
import com.hnucm.c201901020121.Fragment.Lobby.PickUpTakeOutFragment;
import com.hnucm.c201901020121.Fragment.Lobby.PickUpThingsFragment;
import com.hnucm.c201901020121.Fragment.Lobby.PrintFragment;

import java.util.List;

/**
 * Created by Kun on 2016/12/20.
 * GitHub: https://github.com/AndroidKun
 * CSDN: http://blog.csdn.net/a1533588867
 * Description:
 */

public class FragmentAdapter extends FragmentStatePagerAdapter {

    private List<Fragment> mFragments;
    private List<String> mTitles;

    public FragmentAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) {
        super(fm);
        mFragments = fragments;
        mTitles = titles;
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles.get(position);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        Fragment fragment=createFragment(position);
        View view=fragment.getView();
        ((ViewPager) container).addView(view, 0);
        return view;
    }

    public Fragment createFragment(int position) {
        switch (position) {
            case 0:
                return new PickUpThingsFragment();
            case 1:
                return new PrintFragment();
            case 2:
                return new BringFoodFragment();
            case 3:
                return new BringFruitFragment();
            case 4:
                return new PickUpTakeOutFragment();
            default:
                return new PickUpExpressFragment();
        }
    }
}